// pages/shopping/shopping.js
// {
//   "pagePath": "pages/index/index",
//   "iconPath": "images/member.png",
//   "selectedIconPath": "images/member1.png",
//   "text": "会员"
// },
// {
//   "pagePath": "pages/shopping/shopping",
//   "iconPath": "images/member.png",
//   "selectedIconPath": "images/member1.png",
//   "text": "购物"
// },
// {
//   "pagePath": "pages/person/person",
//   "iconPath": "images/person.png",
//   "selectedIconPath": "images/person1.png",
//   "text": "我的"
// }
let myRequest = require('../../common/script/myRequest.js');
let api = require('../../common/script/api.js');
let login = require('../../common/script/login');
let utils = require('../../utils/util');
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: null,
    goodsClass: null,
    goodsLevel2Class: null,
    goodsList: null,
    http: api.http,
    nav1: '',
    nav2: '',
    scrollBool: false,
    scrollTop: 0,
    noData: false,
    isEnd: false,
    page: 1,
    limit: 8,
    showTop: false,
    topNum: 0

  },
  //活动页面
  toActivity(e) {
    let redirect_type = e.currentTarget.dataset.redirect_type;
    let content = e.currentTarget.dataset.content;
    let aid = e.currentTarget.dataset.aid;
    switch (redirect_type) {
      case 0: // 不跳转
        break;
      case 1: // 跳转详情页
        wx.navigateTo({
          url: `/goods/pages/goodsDetails/goodsDetails?goodsId=${content}`
        })
        break;
      case 5: // 跳转优惠券页
        wx.navigateTo({
          url: `/goods/pages/coupon/coupon`
        })

        break;
      case 6: // 跳转活动页
        wx.navigateTo({
          url: `/goods/pages/activity/activity?goods_ids=${aid}`
        })
        break;
    }
  },
  //搜索
  search() {
    wx.navigateTo({
      url: `/goods/pages/search/search`
    })
  },
  toTop() {
    const that = this;
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })

    that.setData({
      topNum: 0,
      showTop: false
    })
  },
  getData() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.newHomePage).then(res => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        that.setData({
          formData: res.data.result
        })
        that.getStoreClassData(res.data.result.select_store_id)
        that.getScrollTop()
        wx.setStorageSync('select_store_id', res.data.result.select_store_id)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  getScrollTop() {
    const that = this;
    const query = wx.createSelectorQuery()
    query.selectAll('.main').boundingClientRect()
    query.selectViewport().scrollOffset();
    query.exec(function (res) {
      that.setData({
        scrollTop: res[0][0].height
      })

    });
  },
  getStoreClassData(storeId) {
    const that = this;
    let data = {
      select_store_id: storeId
    }
    myRequest.EasyHttp.post(api.reqUrls.getStoreClass, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          goodsClass: res.data.result,
          goodsLevel2Class: res.data.result[res.data.result[0][0].cat_id],
          nav1: res.data.result[0][0].cat_id,
          nav2: res.data.result[res.data.result[0][0].cat_id][0].cat_id
        })
        that.getGoods(res.data.result[0][0].cat_id, res.data.result[res.data.result[0][0].cat_id][0].cat_id)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  getGoods(cat1, cat2) {
    const that = this;
    let data = {
      cat_id1: cat1,
      cat_id2: cat2,
      page: that.data.page,
      limit: that.data.limit
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsByLevel2Cat, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.length == 0) {
          that.setData({
            noData: true
          })
        } else {
          that.setData({
            noData: false
          })
        }
        that.setData({
          goodsList: res.data.result
        })
      }
    })
  },
  toGoods(e) {
    const that = this;
    let goodsId = e.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: `/goods/pages/goodsDetails/goodsDetails?goodsId=${goodsId}`,
    })
  },
  toCart() {
    wx.navigateTo({
      url: '/goods/pages/shoppingCart/shoppingCart',
    })
  },
  debounces: utils.throttle(function (e) {
    this.addCart(e[0])
  }, 1000),
  // 加入购物车
  addCart(e) {
    const that = this;
    let goodId = e.currentTarget.dataset.goods_id
    let data = {
      goods_num: 1,
      goods_id: goodId,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.addCart, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showModal({
          showCancel: false,
          content: res.data.message,
          success(res) {
            if (res.confirm) {

            }
          }
        })
      }
    })
  },
  //点击一级分类
  clickNav1(e) {
    const that = this;
    let nav1Id = e.currentTarget.dataset.catid
    that.setData({
      isEnd: false,
      noData: false,
      page: 1,
      goodsLevel2Class: that.data.goodsClass[nav1Id],
      nav1: nav1Id,
      nav2: that.data.goodsClass[nav1Id][0].cat_id,
      topNum: 0
    })

    that.getGoods(nav1Id, that.data.goodsClass[nav1Id][0].cat_id)
  },
  //点击二级分类
  clickNav2(e) {
    const that = this;
    let nav1Id = that.data.nav1
    let nav2Id = e.currentTarget.dataset.catid
    that.setData({
      isEnd: false,
      noData: false,
      page: 1,
      nav2: nav2Id,
      topNum: 0
    })
    that.getGoods(nav1Id, nav2Id)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getData()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    that.setData({
      noData: false,
      isEnd: false,
      page: 1
    })
    that.getData()
  },
  onPageScroll: function (e) {
    const that = this;
    // 页面滚动时触发
    if (e.scrollTop < that.data.scrollTop - 40) {
      that.setData({
        scrollBool: false,
        showTop: false,
      })
    } else {
      that.setData({
        scrollBool: true,
        showTop: true,
      })
    }

  },
  scrollNum(e) {
    const that = this;
    that.setData({
      topNum: e.detail.scrollTop
    })
  },
  lower(e) {
    const that = this;
    if (!that.data.isEnd) {
      wx.showLoading({
        title: '加载中...',
      })
      let page = that.data.page;
      page++;
      let data = {
        cat_id1: that.data.nav1,
        cat_id2: that.data.nav2,
        limit: that.data.limit,
        page: page,
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.getGoodsByLevel2Cat, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          if (res.data.result.length == 0) {
            that.setData({
              isEnd: true
            })

          } else {
            let goodsList = [...that.data.goodsList, ...res.data.result]
            that.setData({
              page,
              goodsList
            })

          }
        }
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})